// utils_linux.go - 3GPP TS 29.244 GTP-U UP plug-in
//
// Copyright (c) 2021 Travelping GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build linux

package traffic

import (
	"net"

	"github.com/pkg/errors"

	"golang.org/x/sys/unix"
)

func noMTUDiscovery(uc *net.UDPConn) (*net.UDPConn, error) {
	f, err := uc.File()
	if err != nil {
		return nil, errors.Wrap(err, "failed to retrieve file from the conn")
	}
	defer f.Close()

	if err := unix.SetsockoptInt(int(f.Fd()), unix.IPPROTO_IP, unix.IP_MTU_DISCOVER, unix.IP_PMTUDISC_DONT); err != nil {
		return nil, errors.Wrap(err, "setsockopt")
	}

	fc, err := net.FileConn(f)
	if err != nil {
		return nil, errors.Wrap(err, "FileConn")
	}
	return fc.(*net.UDPConn), nil
}
